package Model;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.ArrayList;
/**
 *
 * @author Tran Thuy Duong
 */
public class M_News {
    private int MaTT;
    public M_News(){}
    
        private int matt;
        private String tieudett;
        private String mota;
        private String ngaydang;
        private String hinhanh;
    
        private Connection conn;
        
        public M_News(int _matt, String _tieudett, String _mota, String _ngaydang, String _hinhanh){
            this.matt = _matt;
            
            this.tieudett = _tieudett;
            this.mota = _mota;
            this.ngaydang = _ngaydang;
            this.hinhanh = _hinhanh;
                   
        }
        
        public void connect(){
         try {
            String userName = "root"; // root là mặc định của MySQL.
            String passWord = "";
            String url = 
            "jdbc:mysql://localhost:3306/handmade?useUnicode=true&characterEncoding=UTF-8"; 
           
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            setConn(DriverManager.getConnection(url, userName, passWord));
            }
            catch (Exception e) {
                System.out.println("Diver sai");
            }
        
        }

    /**
     * @return the matt
     */
    public int getMatt() {
        return matt;
    }

    /**
     * @param matt the matt to set
     */
    public void setMatt(int matt) {
        this.matt = matt;
    }

    /**
     * @return the tieudett
     */
    public String getTieudett() {
        return tieudett;
    }

    /**
     * @param tieudett the tieudett to set
     */
    public void setTieudett(String tieudett) {
        this.tieudett = tieudett;
    }

    /**
     * @return the mota
     */
    public String getMota() {
        return mota;
    }

    /**
     * @param mota the mota to set
     */
    public void setMota(String mota) {
        this.mota = mota;
    }

    /**
     * @return the ngaydang
     */
    public String getNgaydang() {
        return ngaydang;
    }

    /**
     * @param ngaydang the ngaydang to set
     */
    public void setNgaydang(String ngaydang) {
        this.ngaydang = ngaydang;
    }

    /**
     * @return the hinhanh
     */
    public String getHinhanh() {
        return hinhanh;
    }

    /**
     * @param hinhanh the hinhanh to set
     */
    public void setHinhanh(String hinhanh) {
        this.hinhanh = hinhanh;
    }

    /**
     * @return the conn
     */
    public Connection getConn() {
        return conn;
    }

    /**
     * @param conn the conn to set
     */
    public void setConn(Connection conn) {
        this.conn = conn;
    }

      public M_News[] show_News(){
            connect();
            ResultSet rs=null;
            M_News[] result=null;
            try{
            String sql_show_KH = "select *from tintuc"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_show_KH);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int matt = rs.getInt("MaTT");
                String strtieudett = rs.getString("TieuDeTT");
                String strmota = rs.getString("MoTa");
                String strngaydang = rs.getString("NgayDang");
                String strhinhanh = rs.getString("HinhAnh");
                
                M_News n = new M_News(matt, strtieudett, strmota, strngaydang, strhinhanh);
                list.add(n);
                
                
            }
            result = new M_News[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
      
      //Show_User_News
      
      
      
   /*   
    public String createMaTT (){
        connect();
        java.sql.Statement stmt = null;
        ResultSet rs = null;
        String MA = "";
        
       try {
            
            String sql = "SELECT * FROM tintuc";
         
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            int dem = 1;
            while(rs.next()) { // Duyệt RecordSet
                dem++;
            } 
            MA = Integer.toString(dem);
           
            stmt.close();
            rs.close();
            conn.close();
            return MA;
            
        } catch (Exception e) {
            e.printStackTrace();
        }
       return MA;
    }*/
     public boolean delete_News(){
            connect();
            
           //DELETE FROM `danhmucsp` WHERE 1
          try{
            String sql_delete_News = "Delete from tintuc Where MaTT=?"; 
               
            PreparedStatement stm = conn.prepareStatement(sql_delete_News);
            stm.setInt(1, matt);
            
            int val= stm.executeUpdate();
            
            stm.close();
            conn.close();
            if(val>0)
                return true;
           
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
          return false;
   }
     
     public boolean insert_News(String tieudett, String mota, String ngaydang, String hinhanh){
            connect();
            
            ResultSet rs = null;
            boolean result = false;
           
          try{
            String sql_insert_News = "insert into tintuc(TieuDeTT, MoTa, NgayDang, HinhAnh) values("; 
            
            sql_insert_News += "'"+ tieudett +"',"; 
            
            sql_insert_News += "'"+ mota +"',";
            sql_insert_News += "'"+ ngaydang +"',";
            sql_insert_News += "'"+ hinhanh +"')";


            Statement st = conn.createStatement();
            int val= st.executeUpdate(sql_insert_News);
            result=true;
            }
            catch (SQLException s){
                System.out.println("SQL insert tintuc statement is not executed!"+ s.getMessage());
            }
          return result;
   }

     
     public boolean update_News(int MaTT, String TieuDeTT, String MoTa, String NgayDang, String HinhAnh){
            connect();
           
          try{
            
            String sql_edit_News = "update TINTUC set TieuDeTT=?, MoTa=?, NgayDang=?, HinhAnh=? where MaTT=?"; 
            PreparedStatement stm = conn.prepareStatement(sql_edit_News);
           
            stm.setString(1, TieuDeTT);
            stm.setString(2, MoTa);
            stm.setString(3, NgayDang);
            stm.setString(4, HinhAnh);
             stm.setInt(5, MaTT);
          
           
            int val= stm.executeUpdate();
            stm.close();
            conn.close();
           if(val>0)
                return true;
        
            }
            catch (SQLException s){
                System.out.println("SQL update TINTUC statement is not executed!"+ s.getMessage());
            }
          return false;
   }
     
     public M_News[] search_News(String search){
            connect();
            ResultSet rs = null;
            M_News[] result=null;
            try{
            String sql_search_News = "select *from tintuc where TieuDeTT like '%" + search + "%' or  MoTa like '%" + search + "%' or NgayDang like '%"  + search + "%'"; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_News);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int matt = rs.getInt("MaTT");
                String tieudett = rs.getString("TieuDeTT");
                String mota = rs.getString("MoTa");
                String ngaydang = rs.getString("NgayDang");
                String hinhanh = rs.getString("HinhAnh");
               
                M_News n = new M_News(matt, tieudett, mota, ngaydang, hinhanh);
                list.add(n);  
              
            }
            result = new M_News[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
     
     /*
     public M_News[] search_News(String search){
            connect();
            ResultSet rs = null;
            M_News[] result=null;
            try{
                
            String sql_search_News = "select *from tintuc where MaTT like '%" + search + "%' or TieuDeTT like '%" + search + "%' or MoTa like '%" + search + "%' or NgayDang like '%" + search + "%' or HinhAnh like '%" + search + "%'" ; 
           
            Statement st = conn.createStatement();
            rs= st.executeQuery(sql_search_News);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                String strmatt = rs.getString("MaTT");
                String strtieudett = rs.getString("TieuDeTT");
                String strmota = rs.getString("MoTa");
                String strngaydang = rs.getString("NgayDang");
                String strhinhanh = rs.getString("HinhAnh");
                
                M_News news = new M_News(strmatt, strtieudett, strmota, strngaydang, strhinhanh);
                list.add(news);  
                
                
            }
            result = new M_News[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            conn.close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
     */

    
}
